package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.LabelRelationMapper;
import com.example.communitypro.pojo.Comment;
import com.example.communitypro.pojo.Label;
import com.example.communitypro.pojo.LabelRelation;
import com.example.communitypro.pojo.Post;
import com.example.communitypro.service.LabelRelationService;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LabelRelationServiceImpl implements LabelRelationService {
    @Autowired(required=false)
    private LabelRelationMapper labelRelationMapper;
    @Override
    public Result addLabelRelation(LabelRelation record) {
        try {

            if(labelRelationMapper.insertSelective(record)>0)
            {
                return Result.ok().message("新增关联标签成功");
            }
            else{
                return Result.error().message("新增关联标签失败");
            }
        }catch (Exception e){
            return Result.error().message("新增关联标签失败");
        }
    }

    @Override
    public Result dropLabelRelation(Integer relationNo) {
        try {

            if(labelRelationMapper.deleteByPrimaryKey(relationNo)>0)
            {
                return Result.ok().message("删除关联标签成功");
            }
            else{
                return Result.error().message("删除关联标签失败");
            }
        }catch (Exception e){
            return Result.error().message("删除关联标签失败");
        }
    }

    @Override
    public Result modifyLabelRelation(LabelRelation record) {
        return null;
    }

    @Override
    public Result queryLabelRelation(Integer relationNo) {
        try {
            LabelRelation labelRelation=labelRelationMapper.selectByPrimaryKey(relationNo);
            if(labelRelation!=null)
            {
                return Result.ok().message("查找关联标签成功").data("labelRelation",labelRelation);
            }
            else{
                return Result.error().message("查找关联标签失败");
            }
        }catch (Exception e){
            return Result.error().message("查找关联标签失败");
        }
    }

    @Override
    public Result queryByOb(Integer obNo, Integer obType) {
        try {
            List<Label> labels=labelRelationMapper.selectByOb(obNo,obType);
            if(labels!=null)
            {
                return Result.ok().message("找到对象标签成功").data("labels",labels);
            }
            else{
                return Result.error().message("找到对象所有标签失败");
            }
        }catch (Exception e){
            return Result.error().message("找到对象所有标签失败");
        }
    }

    @Override
    public Result changeLabelRe(List<LabelRelation> records) {
        try {
            for (LabelRelation labelRelation:records) {
                LabelRelation label = labelRelationMapper.judgeLabel(labelRelation.getObNo(), labelRelation.getLabelNo(),labelRelation.getObType());
                if (label != null) {
                    labelRelationMapper.deleteByPrimaryKey(label.getRelationNo());
                } else {
                    labelRelationMapper.insertSelective(labelRelation);
                }
            }
            return Result.ok().message("对象标签操作成功");

        }catch (Exception e){
            return Result.error().message("对象标签操作失败");
        }
    }


    @Override
    public Result queryObByLabel(Integer labelNo) {
        try {
            List<Post> posts=labelRelationMapper.selectPostByLabel(labelNo);
            if(posts!=null)
            {
                return Result.ok().message("找到所有标签帖子成功").data("posts",posts);
            }
            else{
                return Result.error().message("找到所有标签帖子失败");
            }
        }catch (Exception e){
            return Result.error().message("找到所有标签帖子失败");
        }
    }

    @Override
    public Result recommendPost(Integer userNo){
        try{
            List<Label> labels = labelRelationMapper.selectByOb(userNo,2);
            labels = labels.stream().sorted(Comparator.comparing(Label::getLabelHot)).collect(Collectors.toList());
            List<Post> posts = new ArrayList<>();
            for (Label label : labels){
                posts.addAll(labelRelationMapper.selectPostByLabel(label.getLabelNo()));
            }
            if (posts != null){
                return Result.ok().message("推荐成功").data("posts",posts);
            }
            else
                return Result.ok().message("推荐失败").data("posts",posts);
        }catch (Exception e){
            return  Result.error().message("推荐操作失败");
        }
    }
}
